
package network.httpsServer;

import java.util.Date;

import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;

import network.utils.ProxyConstants;

/**
 * Local Https Server. User first connects to this SSL server through SSL tunnel
 * proxy.
 * 
 * @author http://arunava.in
 * 
 */
public class HttpsServerStarter_8443 extends Thread {
	public static int started = 0;

	public static void main(final String[] args) {
		// Set SSL Certificate .
		System.setProperty("javax.net.ssl.keyStore",
				ProxyConstants.KEYSTORE_PATH);
		System.setProperty("javax.net.ssl.keyStorePassword",
				ProxyConstants.KEYSTORE_PASSWORD);
		/*
		 * System.setProperty("javax.net.ssl.trustStore", "c:\\proxy_trust");
		 * System.setProperty("javax.net.ssl.trustStorePassword", "aaaaaaa");
		 */
		new HttpsServerStarter_8443().run();
	}

	public void run() {
		if (ProxyConstants.logLevel >= 1) {
			System.out.println(new Date()
					+ " HttpsServerStarter :: HTTPS-Server started :: on "
					+ ProxyConstants.HTTPSPort_8443);
		}
		try {

			if (ProxyConstants.logLevel >= 1) {
				System.out.println(new Date()
						+ " HttpsServerStarter :: Init HttpsServer ...... ");
			}
			final SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory
					.getDefault();
			final SSLServerSocket sslserversocket = (SSLServerSocket) sslserversocketfactory
					.createServerSocket(ProxyConstants.HTTPSPort_8443);

			while (true) {
				started = 1;
				final SSLSocket sslsocket = (SSLSocket) sslserversocket
						.accept();
				new HttpsServerThread(sslsocket, 8443).start();
				System.out
						.println(new Date()
								+ " HttpsServerStarter :: Client connected to HttpsServer :: "
								+ sslsocket.getRemoteSocketAddress());
			}
		} catch (final Exception e) {
			started = 2;
			System.out.println(e);
		}
	}
}
